# 热题100 https://leetcode.cn/studyplan/top-100-liked/
# 1/100 哈希-两数之和
# leetcode第1题: https://leetcode.cn/problems/two-sum/description/?envType=study-plan-v2&envId=top-100-liked
# Date: 2024/11/13
# 同seq 中的 lc1
def twoSum(nums: list[int], target: int) -> list[int]:
    n = len(nums)
    if n == 2:
        if nums[0] + nums[1] == target:
            return [nums[0], nums[1]]
        else:
            return []
    mapping = {}
    for i in range(n):
        if target - nums[i] in mapping:
            return [mapping[target - nums[i]], i]
        else:
            mapping[nums[i]] = i
    return []


if __name__ == '__main__':
    print(twoSum([2, 7, 11, 15], 9))  # [0,1]
    print(twoSum([3, 2, 4], 6))  # [1,2]
